<?php
namespace App\Models\PartnerModels;
use App\Models\ParentModel;
use DB;
use App\Components\Tools;
/**
 * 实名理model
 * @author puppet
 */
class RealName extends ParentModel{
  /**
   * 与模型关联的数据表。
   *
   * @var string
   */
  protected $table = 'pt_real_name';

  /**
   * DB::直接操作的数据库表名
   * @var string
   */
  protected $db_table = 'pt_real_name';

  /**
   * 主键 默认id。
   *
   * @var string
   */
  protected $primaryKey = 'id';

  /**
   * 指定是否模型应该被戳记时间。
   *
   * @var bool
   */
  public $timestamps = false;

  //查询是否实名
    public function  getRealName($params){

        $sql =" select status,Business_license,sfz_zm_url,sfz_fm_url,create_time,update_time from pt_real_name  where  pid=$params";
        $sql.=" group by id order by update_time desc ";
        $res=DB::select($sql);
   //print_r($res);exit();
        if(empty($res)){//没查到数据
            $res[0]['zt']='暂未实名';
            $res[0]['Business_license']='没有上传图片';
            $res[0]['sfz_zm_url']='没有上传图片';
            $res[0]['sfz_fm_url']='没有上传图片';
        }elseif ($res[0]['status']==-1){
            $res[0]['zt']='审核不通过';
        }elseif ($res[0]['status']==1){
            $res[0]['zt']='审核中';
        }elseif ($res[0]['status']==2){
            $res[0]['zt']='审核成功';
        }

        return $res;
    }


//查询未审核数据
function getPageData($params){
    $qiniuUrl=Tools::getOneconfig("/config/third.json",array('qiniu','qnUrl'));
    // echo "<pre>";print_r($params);exit();
    $size = (isset($params['limit']) && intval($params['limit'])>0 && intval($params['limit']) <= 100) ? $params['limit'] : 5;
    $page = isset($params['page']) ? intval($params['page']) : 1;
    $offset = ($page - 1) * $size;

    $search_name=isset($params['search_name']) ? $params['search_name'] : '';
    $search_company=isset($params['company']) ? $params['company'] : '';
    $search_status=isset($params['search_status']) ? intval($params['search_status']) : 99;
    $search_user=isset($params['search_user']) ? $params['search_user'] : '';
    $countSql="select count(a.id) as count from $this->table as a left join pt_partner as b on b.id=a.pid where 1=1";
    $sql = "SELECT a.id,a.pid,a.Business_license,a.remark,a.sfz_zm_url,a.sfz_fm_url,a.create_time,a.update_time,a.type,a.status,b.user_name,b.company,b.city,b.name,b.gender,b.customertype from $this->table as a left join pt_partner as b on b.id=a.pid ";
    $sql.=" where 1=1 ";

    if($search_name){
        $countSql.=" and b.user_name like '%{$search_name}%'";
        $sql.=" and b.user_name like '%{$search_name}%'";
    }

    if($search_company){
        $countSql.=" and b.company like '%{$search_company}%'";
        $sql.=" and b.company like '%{$search_company}%'";
    }
    if($search_status!=99){
        $countSql.=" and a.status = {$search_status}";
        $sql.=" and a.status = {$search_status}";
    }
    $sql.=" order by a.create_time desc limit $offset,$size";
    // echo $sql;exit;
    $count=DB::select($countSql);
    $count = isset($count[0]['count']) ? $count[0]['count'] : 0;
    $data=DB::select($sql);
    //print_r($data);
    if(!empty($data)){
        $i=0;
        foreach($data as $k=>$v){
            ++$i;
            $data[$k]['kid']=$i;
            $data[$k]['Business_license']="<a href='{$qiniuUrl}/{$v['Business_license']}' target='_blank'><img src='{$qiniuUrl}/{$v['Business_license']}' style='width:80px;height:100px;'></a>";
            $data[$k]['sfz_zm_url']="<a href='{$qiniuUrl}/{$v['sfz_zm_url']}' target='_blank'><img src='{$qiniuUrl}/{$v['sfz_zm_url']}' style='width:180px;height:100px;'></a>";
            $data[$k]['sfz_fm_url']="<a href='{$qiniuUrl}/{$v['sfz_fm_url']}' target='_blank'><img src='{$qiniuUrl}/{$v['sfz_fm_url']}' style='width:180px;height:100px;'></a>";
            $data[$k]['user_name']=$v['user_name'];
            $data[$k]['company']=$v['company'];
            $data[$k]['name']=$v['name'];
            if($v['gender']==1){
                $data[$k]['gender']='男';
            }elseif ($v['gender']==2){
                $data[$k]['gender']='女';
            }else{
                $data[$k]['gender']='未知';
            }
            if($v['customertype']==1){
                $data[$k]['customertype']='公司';
            }elseif ($v['customertype']==2){
                $data[$k]['customertype']='个体户';
            }else{
                $data[$k]['customertype']='未知';
            }
            $data[$k]['remark']="<div style='width:100px;height:100px;overflow:auto;'>{$v['remark']}</div>";
            $data[$k]['name']="<div style='width:100px;height:100px;overflow:auto;'>{$v['name']}</div>";
            if(!empty($v['operator'])){
                $data[$k]['operator']= $v['operator'];
            }else{
                $data[$k]['operator']='系统审核';
            }

            if($v['status']==1){
                $data[$k]['status']="<span style='padding:5px 8px;color:#fff;border:1px solid transparent;background:#00c0ef;border-radius:13px;'>审核中</span>";
            }elseif($v['status']==2){
                $data[$k]['status']="<span style='padding:5px 8px;color:#fff;border:1px solid transparent;background:#00a65a;border-radius:13px;'>实名通过</span>";
            }elseif($v['status']==-1){
                $data[$k]['status']="<span style='padding:5px 8px;color:#fff;border:1px solid transparent;background:#dd4b39;border-radius:13px;'>实名失败</span>";
            }else{
                $data[$k]['status']="<span style='padding:5px 8px;color:#fff;border:1px solid transparent;background:#d2d6de;border-radius:13px;'>未审核</span>";
            }
            $data[$k]['create_time']=$v['create_time'];
            if($v['status']==1){
                $data[$k]['operation']='<button class="btn btn-info btn-sm" onclick="saveVoid('."'{$v['id']}','1'".')">通过</button>';
                $data[$k]['operation'].="<br/><br/>";
                $data[$k]['operation'].='<button class="btn btn-defult btn-sm" onclick="saveVoid('."'{$v['id']}','-1'".')">不通过</button>';
            }else{
                $data[$k]['operation']='<button class="btn btn-danger btn-sm" onclick="saveVoid('."'{$v['id']}','1'".')" disabled>通过</button>';
                $data[$k]['operation'].="<br/><br/>";
                $data[$k]['operation'].='<button class="btn btn-defult btn-sm" onclick="saveVoid('."'{$v['id']}','-1'".')" disabled>不通过</button>';
            }
//            $data[$k]['operation']='<button class="btn btn-info btn-sm" onclick="saveVoid('."'{$v['id']}','1'".')">通过</button>';
//            $data[$k]['operation'].="<br/><br/>";
//            $data[$k]['operation'].='<button class="btn btn-defult btn-sm" onclick="saveVoid('."'{$v['id']}','-1'".')">不通过</button>';
        }
    }
    return ['data'=>$data,'total'=>$count];
}

//修改状态
function updateAll($kid,$data){
    $sql="update $this->table set ";
    if(is_array($data)){
        foreach($data as $k=>$v){
            $sql.="`{$k}`='{$v}',";
        }
        $sql=substr($sql,0,-1)." where id={$kid}";
    }
    // echo $sql;exit;
    return DB::update($sql);
}
//通过id获取pid
    function getpid($kid)
    {
        $sql=" select  pid from $this->table where id=$kid";
        $pid=DB::select($sql);
        return $pid[0]['pid'];
        }

//修改合伙人/会员表的状态
    function updatepattnerAll($kid,$data){
        $sql="update pt_partner set ";
        if(is_array($data)){
            foreach($data as $k=>$v){
                $sql.="`{$k}`='{$v}',";
            }
            $sql=substr($sql,0,-1)." where id={$kid}";
        }
        // echo $sql;exit;
        return DB::update($sql);
    }

//查询视频信息
function selectVideo($kid){
    $re=DB::select("select * from {$this->table} where id = ?",[$kid]);
    $re=isset($re[0]['id']) ? $re[0]: false;
    return $re;
}

//查询用户授权信息
function selectToken($open_id){
    $re=DB::select("select * from pt_dy_access_token where open_id = ? and status = 1",[$open_id]);
    $re=isset($re[0]['id']) ? $re[0] : false;
    return $re;
}

//实名审核列表
function getPageDataAll($params){
    $qiniuUrl=Tools::getOneconfig("/config/third.json",array('qiniu','qnUrl'));
    $size = (isset($params['limit']) && intval($params['limit'])>0 && intval($params['limit']) <= 100) ? $params['limit'] : 5;
    $page = isset($params['page']) ? intval($params['page']) : 1;
    $offset = ($page-1) * $size;

    $search_user=isset($params['search_user']) ? $params['search_user'] : '';
    $search_company=isset($params['search_company']) ? $params['search_company'] : '';
    $search_name=isset($params['search_name']) ? $params['search_name'] : '';
    $search_status=isset($params['search_status']) ? intval($params['search_status']) : 99;
    $search_user=isset($params['search_user']) ? $params['search_user'] : '';
    $countSql="select count(a.id) as count from $this->table as a left join pt_partner as b on b.id=a.pid where 1 = ?";
    $sql="select a.*,b.user_name,b.company,b.name from $this->table as a left join pt_partner as b on b.id=a.pid where 1 = ?";

    if($search_user){
        $countSql.=" and b.user_name like '%{$search_user}%'";
        $sql.=" and b.user_name like '%{$search_user}%'";
    }

    if($search_company){
        $countSql.=" and b.company like '%{$search_company}%'";
        $sql.=" and b.company like '%{$search_company}%'";
    }

    if($search_name){
        $countSql.=" and b.name like '%{$search_name}%'";
        $sql.=" and b.name like '%{$search_name}%'";
    }

    if($search_status!=99){
        $countSql.=" and a.status = {$search_status}";
        $sql.=" and a.status = {$search_status}";
    }
    // echo $search_status;
    $sql.=" order by a.update_time desc limit $offset,$size";
    // echo $sql;exit;
    $count=DB::select($countSql,[1]);
    $count = isset($count[0]['count']) ? $count[0]['count'] : 0;
    $data=DB::select($sql,[1]);
   //echo print_r($data);exit();
    if(!empty($data)){
        $i=0;
        foreach($data as $k=>$v){
            ++$i;
            $data[$k]['kid']=$i;
            $data[$k]['Business_license']="<a href='{$qiniuUrl}/{$v['Business_license']}' target='_blank'><img src='{$qiniuUrl}/{$v['Business_license']}' style='width:80px;height:100px;'></a>";
            $data[$k]['sfz_zm_url']="<a href='{$qiniuUrl}/{$v['sfz_zm_url']}' target='_blank'><img src='{$qiniuUrl}/{$v['sfz_zm_url']}' style='width:180px;height:100px;'></a>";
            $data[$k]['sfz_fm_url']="<a href='{$qiniuUrl}/{$v['sfz_fm_url']}' target='_blank'><img src='{$qiniuUrl}/{$v['sfz_fm_url']}' style='width:180px;height:100px;'></a>";
            $data[$k]['user_name']=$v['user_name'];
            $data[$k]['company']=$v['company'];
            $data[$k]['remark']="<div style='width:100px;height:100px;overflow:auto;'>{$v['remark']}</div>";
            $data[$k]['name']="<div style='width:100px;height:100px;overflow:auto;'>{$v['name']}</div>";
            if(!empty($v['operator'])){
                $data[$k]['operator']= $v['operator'];
            }else{
                $data[$k]['operator']='系统审核';
            }

            if($v['status']==1){
                $data[$k]['status']="<span style='padding:5px 8px;color:#fff;border:1px solid transparent;background:#00c0ef;border-radius:13px;'>审核中</span>";
            }elseif($v['status']==2){
                $data[$k]['status']="<span style='padding:5px 8px;color:#fff;border:1px solid transparent;background:#00a65a;border-radius:13px;'>实名通过</span>";
            }elseif($v['status']==-1){
                $data[$k]['status']="<span style='padding:5px 8px;color:#fff;border:1px solid transparent;background:#dd4b39;border-radius:13px;'>实名失败</span>";
            }else{
                $data[$k]['status']="<span style='padding:5px 8px;color:#fff;border:1px solid transparent;background:#d2d6de;border-radius:13px;'>未审核</span>";
            }
            $data[$k]['update_time']="<div style='width:100px;height:100px;overflow:auto;'>{$v['update_time']}</div>";
        }
    }
    return ['data'=>$data,'total'=>$count];
}

}